package com.liunian.algorithmstudy.tree;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;

public class RightSideView199 extends TreeNode {

	public List<Integer> rightSideView(TreeNode root) {
		if (root == null) {
			return Collections.emptyList();
		}
		Deque<TreeNode> deque = new LinkedList<>();
		List<Integer> res = new ArrayList<>();
		deque.push(root);
		while (!deque.isEmpty()) {
			int size = deque.size();
			while (size-- > 0) {
				TreeNode treeNode = deque.pollFirst();
				if (size == 0) {
					res.add(treeNode.val);
				}
				if (treeNode.left != null) {
					deque.addLast(treeNode.left);
				}
				if (treeNode.right != null) {
					deque.addLast(treeNode.right);
				}
			}
		}
		return res;
	}

}
